package name.fowler.refactoring.simplestore;

import name.fowler.refactoring.simplestore.model.Customer;
import name.fowler.refactoring.simplestore.model.Rental;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jlebedeva
 */
public class ReportService {

    private List<String> statementTemplate = Arrays.asList(
            "Rental record for $0\n",
            "\t$1\t$2\n",
            "Amount owed is $3\n", 
            "You earned $4 frequent renter points.");
    private static final String[] STATEMENT_VARIABLES = {"$0", "$1", "$2", "$3", "$4"};

    public Report getStatementReport(Customer customer) {
        List<String> movieNames = new ArrayList<String>();
        List<Double> moviePrices = new ArrayList<Double>();
        double totalAmount = 0;
        int frequentRenterPoints = 0;
        for (Rental rental : customer.getRentals()) {
            movieNames.add(rental.getMovie().getTitle());
            moviePrices.add(rental.getTotalPrice());
            totalAmount += rental.getTotalPrice();
            frequentRenterPoints += rental.getFrequentPoints();
        }
        Report report = new Report(statementTemplate, STATEMENT_VARIABLES);
        report.replace("$0", customer.getName());
        report.replace("$1", movieNames);
        report.replace("$2", moviePrices, false);
        report.replace("$3", totalAmount);
        report.replace("$4", frequentRenterPoints);
        return report;
    }

    public List<String> getStatementTemplate() {
        return statementTemplate;
    }

    public void setStatementTemplate(List<String> statementTemplate) {
        this.statementTemplate = statementTemplate;
    }
}
